using System;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;

namespace PreguntaAlExperto.DataFramework
{
    internal class ConfigurationHandler
    {
        /// <summary>
        /// Returns the Connection String stored on the Web.Config file.
        /// </summary>
        public static string getConnectionString
        {
            get { return ConfigurationManager.ConnectionStrings["DataBaseConnection"].ConnectionString.ToString(); }
        }

        /// <summary>
        /// Returns the requested query stored in the Framework.xml
        /// </summary>
        /// <param name="querySection">String value representing the name of the query to search</param>
        /// <returns></returns>
        public static string getQuery(string querySection)
        {
            string _query;
            XmlDocument queries = new XmlDocument();
            queries.Load(ConfigurationManager.AppSettings["FrameworkPath"].ToString());

            string xExpression = "//query[@name='" + querySection + "']";
            XmlElement queryItem = (XmlElement)queries.SelectSingleNode(xExpression);

            if (queryItem != null)
                _query = queryItem.GetAttribute("action");
            else
                return "";

            return _query;

        }

        public static Dictionary<string, string> getParameter(string querySection)
        {
            Dictionary<string, string> _parameters = new Dictionary<string, string>();
            XmlDocument queries = new XmlDocument();
            queries.Load(ConfigurationManager.AppSettings["FrameworkPath"].ToString());

            string xExpression = "//query[@name='" + querySection + "']";
            XmlElement queryItem = (XmlElement)queries.SelectSingleNode(xExpression);

            if (queryItem != null)
            {
                foreach (XmlNode x in queryItem.ChildNodes)
                    _parameters.Add(x.Attributes["name"].Value.ToString(), x.Attributes["type"].Value.ToString());
            }

            return _parameters;
        }

        public static Dictionary<string, string> getParameterSize(string querySection)
        {
            Dictionary<string, string> _parameters = new Dictionary<string, string>();
            XmlDocument queries = new XmlDocument();
            queries.Load(ConfigurationManager.AppSettings["FrameworkPath"].ToString());

            string xExpression = "//query[@name='" + querySection + "']";
            XmlElement queryItem = (XmlElement)queries.SelectSingleNode(xExpression);

            if (queryItem != null)
            {
                foreach (XmlNode x in queryItem.ChildNodes)
                {
                    try
                    {
                        _parameters.Add(x.Attributes["name"].Value.ToString(), x.Attributes["size"].Value.ToString());
                    }
                    catch
                    {
                        _parameters.Add(x.Attributes["name"].Value.ToString(), null);
                    }
                }
            }

            return _parameters;
        }

        public static string getQueryType(string querySection)
        {
            string _parameter = "";
            XmlDocument queries = new XmlDocument();
            queries.Load(ConfigurationManager.AppSettings["FrameworkPath"].ToString());

            string xExpression = "//query[@name='" + querySection + "']";
            XmlElement queryItem = (XmlElement)queries.SelectSingleNode(xExpression);

            if (queryItem != null)
            {
                foreach (XmlAttribute x in queryItem.Attributes)
                {
                    if (x.Name == "type")
                    {
                        _parameter = x.Value;
                        break;
                    }
                }
            }

            return _parameter;
        }
        
    }
}
